//
//  MyViewController.m
//  BSLCar
//
//  Created by dlios on 15/5/22.
//  Copyright (c) 2015年 lyz. All rights reserved.
//

#import "MyViewController.h"
#import <SDImageCache.h>
#import <UIImageView+WebCache.h>
#import "ViedoViewController.h"
#import "DataBaseManager.h"
#import "MyTableViewCell.h"
#import "XDKAirMenuController.h"

@interface MyViewController () <chuanProCity>
{
    UILabel *label0;
    UIView *view0;
    UIView *view2;
    UILabel *label02;
    UIImageView  *imageView1;
    UILabel *label1;
    UMSocialAccountEntity *snsAccount;
}
@end
@implementation MyViewController
- (void)dealloc
{
    [_dataArray release];
    [super dealloc];
}

- (void)barButtonAction:(UIBarButtonItem *)barButton {
    if ([[XDKAirMenuController sharedMenu] isMenuOpened]) {
        
        [[XDKAirMenuController sharedMenu] closeMenuAnimated];
    } else {
        [[XDKAirMenuController sharedMenu] openMenuAnimated];
    }
}

- (void)viewDidLoad {
    self.navigationItem.title = @"设置";
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] init];
    item.title = @"返回";
    self.navigationItem.backBarButtonItem = item;
    [item release];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(-5, 0, 25, 23);
    [button setImage:[UIImage imageNamed:@"iconfont-caidan.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(barButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] autorelease] initWithCustomView:button];

    
    //建立一个label
    view0 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
    view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
    label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, view2.bounds.size.width - 10, 20)];
    label02 = [[UILabel alloc] initWithFrame:CGRectMake(180, 0, 20, 20)];
    label0 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 180, 20)];
    [super viewDidLoad];
    //计算缓存大小
    UITableView *tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:tableview];
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.delegate = self;
    tableview.dataSource = self;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    view.backgroundColor = [UIColor colorWithRed:0.93 green:0.91 blue:0.81 alpha:1];
//    tableview.tableHeaderView = view;
    imageView1 = [[UIImageView alloc]init];
    imageView1.frame = CGRectMake(self.view.bounds.size.width / 2 - 30, 50, 60, 60);
    [tableview.tableHeaderView addSubview:imageView1];
    imageView1.layer.cornerRadius = 30;
    imageView1.layer.masksToBounds = YES;
    imageView1.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGestrue = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pressed)];
    imageView1.image = [UIImage imageNamed:@"user001@2x"];
    [imageView1 addGestureRecognizer:tapGestrue];
     [view addSubview:imageView1];
    label1 = [[UILabel alloc ]initWithFrame:CGRectMake(self.view.bounds.size.width / 2 - 17, 100, 60, 60)];
    label1.textColor = [UIColor blackColor];
    label1.text = @"登陆";
    [view addSubview:label1];
    UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(0, 165, self.view.frame.size.width - 10, 40)];
    label6.textColor = [UIColor redColor];
    label6.font = [UIFont systemFontOfSize:12];
    label6.alpha = 0.4;
    label6.textAlignment = NSTextAlignmentRight;
    label6.text = @"**目前只支持新浪微博登陆";
    [view addSubview:label6];
    self.dataArray = [NSMutableArray array];
    self.dataArray = [NSMutableArray arrayWithObjects:@"城市选择",@"我的收藏",@"清除缓存",@"精彩视频", nil];
}
- (void)viewWillAppear:(BOOL)animated
{
    //判断之前是否登陆成功过
    if ([DataBaseManager selectWeiBoUserInfo] != nil) {
        WeiboInfoModel *model = [[WeiboInfoModel alloc]init];
        model = [DataBaseManager selectWeiBoUserInfo];
        label1.text = model.username;
        imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.url]]];
    }
}

- (void)pressed
{
    if ([DataBaseManager selectWeiBoUserInfo] == nil){
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        //获取微博用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
          //  NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [DataBaseManager createWeiBoUserInfo];
            WeiboInfoModel *model = [[WeiboInfoModel alloc]init];
            UserInfoModel *userModel = [[UserInfoModel alloc]init];
            userModel.username = snsAccount.userName;
            model.username = snsAccount.userName;
            model.uid = snsAccount.usid;
            model.token = snsAccount.accessToken;
            model.url = snsAccount.iconURL;
            [DataBaseManager insertWeiBoInfo:model];
            [DataBaseManager insert:userModel];
            label1.text = model.username;
            imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.url]]];
        }});
    }
    else
    {
        UIAlertView *alt = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你已经登录过了" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

        [alt show];
       
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (indexPath.row == 0) {
        cell.accessoryView = [self cellAccessoryView];
    }
    cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];
    if (indexPath.row == 2) {
#pragma -mark给label添加内容
        //给label添加内容
        label2.backgroundColor = [UIColor clearColor];
        label2.textAlignment = NSTextAlignmentRight;
        [view2 addSubview:label2];
        //给label添加值,进行逻辑判断
        if ([[SDImageCache sharedImageCache] getSize] != 0) {//有缓存
           CGFloat size = [[SDImageCache sharedImageCache] getSize] / (1024.0 * 1024.0);
            NSString *str2 = [NSString stringWithFormat:@"%.2f %@",size, @"MB"];
                label2.text = str2;
        }
        else{
            label2.text = @"空";
        }
        cell.accessoryView = view2;
    }
    return cell;
}
- (UIView *)cellAccessoryView {
    label0.textAlignment = NSTextAlignmentRight;
    [view0 addSubview:label0];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    label0.text = [user objectForKey:@"proCity"];
    label02.text = @">";
    label02.textAlignment = NSTextAlignmentLeft;
    label02.font = [UIFont boldSystemFontOfSize:20];
    label02.textColor = [UIColor grayColor];
    [view0 addSubview:label02];
    return view0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MyCityViewController *city = [[MyCityViewController alloc]init];
        city.mydelegate = self;
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:city animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }
    if (indexPath.row == 1) {
        ShouCangViewController *shoucang = [[ShouCangViewController alloc]init];
        [self.navigationController pushViewController:shoucang animated:YES];
        
    }
    //点击清除缓存的事件
    else if(indexPath.row == 2){
        if ([[SDImageCache sharedImageCache] getSize]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"确定要清除缓存吗" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"目前没有缓存" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [alert show];
        }
    }
    //精彩视频跳转界面
    if (indexPath.row == 3) {
        ViedoViewController *viedo = [[ViedoViewController alloc] init];
        [self.navigationController pushViewController:viedo animated:YES];
    }
    self.navigationItem.hidesBackButton = YES;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
       if (buttonIndex == 1) {
        //确定
        //清除缓存的语句
        [[[SDWebImageManager sharedManager] imageCache] clearDisk];
        //提示缓存清除完毕，用什么显示
        //动画呢
        UIActionSheet *act = [[UIActionSheet alloc] initWithTitle:@"清除缓存成功" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
        [act showInView:self.view];
        //让定时消失的那个方法；
        [act dismissWithClickedButtonIndex:0 animated:YES];
        label2.text = @"空";
    }
}
- (void)chzhi:(NSString *)st {
    label0.text = st;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
